/*
*编辑私募产品
*EditPrivatePdt.js*/

import React, { PropTypes, PureComponent } from 'react';
import { autobind } from 'core-decorators';
import { Button, Modal, Form, Input, Icon,Select } from 'antd';
import { connect } from 'react-redux';

const Option = Select.Option;
const FormItem = Form.Item;
const EditPrivatePdtForm = Form.create()(
	(props) => {
		const { visible, onCancel, onCreate, form,tag,data} = props;
		const { getFieldDecorator } = form;
		return (
			visible ?
				<Modal
					className={` add-EditPrivatePdt`}
					visible={visible}
					title="编辑"
					okText="提交"
					cancelText="取消"
					onCancel={onCancel}
					onOk={onCreate}
				>
					<Form layout="vertical">
						<FormItem label="产品名称">
              {getFieldDecorator('productName', {
                rules: [{ required: true, message: '请输入产品名称' }],
                initialValue: data.productName,
              })(
                <Input />
              )}
            </FormItem>
            <FormItem label="备案码">
              {getFieldDecorator('recordId', {
                rules: [{ required: true, message: '请输入备案码' }],
                initialValue: data.recordId,
              })(
                <Input />
              )}
            </FormItem>
            <FormItem label="基金ID">
              {getFieldDecorator('windCode', {
								initialValue: data.windCode,
              })(
                <Input disabled/>
              )}
            </FormItem>
           	<FormItem label="所属管理人" >
	            {getFieldDecorator('companyId', {
	            	initialValue: data.companyId,
	              })(
	                <Input disabled/>
	              )}
	            </FormItem>
					</Form>
				</Modal> : null
		);
	},
);
const mapStateToProps = state => ({
	pdtData:state.privatePdt.pdtData,
	managerList:state.privatePdt.managerList
});
const mapDispatchToProps = {
	editPrivatPdt: query => ({
		type: 'privatePdt/editPrivatPdt',
		payload: query || {},
	}),
	
};

@connect(mapStateToProps, mapDispatchToProps)
class EditPrivatePdt extends PureComponent {
	static propTypes = {
		form: PropTypes.object.isRequired,
	}
	constructor(props) {
		super(props);
		this.state = {
			visible: false,
			modalHeight: 0,
		};
	}
	componentDidUpdate() {
		const screenHeight = Math.max(document.documentElement.offsetHeight, document.body.offsetHeight);
		const ele = document.querySelector('.add-EditPrivatePdt .ant-modal-body');
		if (ele) {
			ele.style.height = `${screenHeight * 0.5}px`;
		}
	}

	showModal = () => {
		this.setState({ visible: true });
	}

	handleCancel = () => {
		this.setState({ visible: false });
	}

	handleCreate = () => {
		const form = this.form;
		const {editPrivatPdt,pdtData} = this.props;
		form.validateFields((err, values) => {
			if (err) {
				return;
			}
			console.log('Received values of form:>>>', values);
			pdtData.map((item)=>{
				if(item.windCode == values.windCode){
					values.id = item.productId
				}
			})
			editPrivatPdt({
				...values,
				'productType':'SIMU',
				'isPublic':false
			})
			form.resetFields();
			this.setState({ visible: false });
		});
	}

	saveFormRef = (form) => {
		this.form = form;
	}

	render() {
		const {tag,data} = this.props;
		return (
			<span >
				<a onClick={this.showModal}>编辑</a>
				<EditPrivatePdtForm
					ref={this.saveFormRef}
					visible={this.state.visible}
					onCancel={this.handleCancel}
					onCreate={this.handleCreate}
					tag={tag}
					data={data}
				/>
			</span>
		);
	}
}
export default Form.create({
})(EditPrivatePdt);